package com.cloudservice.ccs.controller;

import com.cloudservice.ccs.biz.INoticeBiz;
import com.cloudservice.ccs.model.ApiResult;
import com.cloudservice.ccs.entity.Notice;
import com.cloudservice.ccs.model.NoticeConnectQuery;
import com.cloudservice.ccs.model.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeBiz noticeBiz;

    @PostMapping("/")
    public ApiResult save(@RequestBody Notice notice) {
        noticeBiz.save(notice);
        return ApiResult.success();
    }

    @PutMapping("/{id}")
    private ApiResult update(@PathVariable Integer id, @RequestBody Notice notice) {
        noticeBiz.update(id, notice);
        Notice result = noticeBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    // @RequiresPermissions("all")
    @PutMapping("/{id}/partial")
    public ApiResult partialUpdate(@PathVariable Integer id, @RequestBody Map<String, Object> fields) {
        noticeBiz.partialUpdate(id, fields);
        Notice result = noticeBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @DeleteMapping("/{id}")
    public ApiResult delete(@PathVariable Integer id) {
        noticeBiz.delete(id);
        return ApiResult.success();
    }

    @DeleteMapping("/{ids}/deletePartial")
    public ApiResult batchDelete(@PathVariable String ids) {
        Arrays.asList(ids.split(",")).forEach(idString -> {
            Integer id = Integer.parseInt(idString);
            noticeBiz.delete(id);
        });
        return ApiResult.success();
    }

    @GetMapping("/{id}")
    public ApiResult queryById(@PathVariable Integer id) {
        Notice result = noticeBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @GetMapping("/")
    public ApiResult queryAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit) {
        PageHelper.startPage(page, limit);
        List<NoticeConnectQuery> result = noticeBiz.getAll();
        return ApiResult.successWithData(PageData.fromPage((Page<NoticeConnectQuery>) result));
    }

    @GetMapping("/{id}/connect")
    public ApiResult queryCustomerService(@PathVariable Integer id){
        NoticeConnectQuery noticeConnectQuery=noticeBiz.queryCustomerService(id);
        return ApiResult.successWithData(noticeConnectQuery);

    }
}
